<?php

namespace App\Services\Member;

use App\Models\Member\CurrencyLog;
use App\Models\Member\Member;
use App\Models\Member\WalletDetail;
use App\Models\Task\Task;
use Illuminate\Support\Facades\DB;

class CurrencyService
{
    /**
     * 收入
     * @param string $member_uuid  用户uuid
     * @param float $currency  圈币
     * @param int $source_type  数据来源类型 100后台管理员增加 101 后台管理员减少
     * @param string $content  具体变动内容说明
     * @return bool  返回值 true成功 false失败
     */
    public static function income(string $member_uuid, float $currency, int $source_type, string $content='', string $img=''){
        try {
            DB::beginTransaction();

            $member = Member::where('uuid', $member_uuid)->first();
            $before_currency = $member->currency;

            //变动用户圈币
            $after_currency = bcadd($member->currency, $currency, 0);
            $member->update([
                'currency' => $after_currency,
            ]);

            switch ($source_type){
                case 100:  //后台管理员增加
                    //增加可用总圈币
                    $after_total_currency = bcadd($member->total_currency, $currency, 2);
                    $member->update([
                        'total_currency' => $after_total_currency
                    ]);
                    break;
            }

            //2.增加圈币日志
            CurrencyLog::create([
                'member_uuid' => $member->uuid,
                'content' => $content,
                'before_currency' => $before_currency,
                'after_currency' => $after_currency,
                'currency' => $currency,
                'type' => 1,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
            ]);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            return false;
        }

        return true;
    }

    /**
     * 支出
     * @param string $member_uuid  用户uuid
     * @param float $currency  圈币
     * @param int $source_type  数据来源类型 100后台管理员增加 101 后台管理员减少
     * @param string $content  具体变动内容说明
     * @param string $img  显示图片
     * @return bool  返回值 true成功 false失败
     */
    public static function out(string $member_uuid, float $currency, int $source_type, string $content='', string $img=''){
        try {
            DB::beginTransaction();
            $member = Member::where('uuid', $member_uuid)->first();
            $before_currency = $member->currency;

            //变动用户金额
            $after_currency = bcsub($member->currency, $currency, 2);
            $member->update([
                'currency' => $after_currency
            ]);

            //日志
            CurrencyLog::create([
                'member_uuid' => $member->uuid,
                'content' => $content,
                'before_currency' => $before_currency,
                'after_currency' => $after_currency,
                'currency' => $currency,
                'type' => 2,
                'month' => date('Y-m-01'),
                'source_type' => $source_type,
            ]);

            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();

            return false;
        }

        return true;

    }

}
